package com.lenl.arithmetic.tenusablealgorithm.kmp;

/**
 * @author Lenl
 * @version v1.0
 * @create 2022-05-14 13:00
 * @description 暴力匹配字符串
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1="dsiqjewqojeqwnfieowjrweqqew";
        String str2="qew";
        int index=violenceMatch(str1,str2);
        System.out.println("index="+index);
    }

    //暴力匹配算法实现
    public static int violenceMatch(String str1,String str2){
        char[] s1=str1.toCharArray();
        char[] s2=str2.toCharArray();

        int s1Len=s1.length;
        int s2Len=s2.length;

        int i=0;//指向s1
        int j=0;//指向s2
        while(i<s1Len&&j<s2Len){
            if(s1[i]==s2[j]){
                //匹配成功
                i++;
                j++;
            }else{
                //未匹配成功
                i=i-j+1;
                j=0;
            }
        }
        if(j==s2Len){
            return i-j;
        }else{
            return -1;
        }


    }

}
